SELECT CASE Statement ---------------------------------------------------------------------------- Action Executes one of several statement blocks depending on the value of an expression. Syntax SELECT CASE testexpression CASE expressionlist1 statementblock-1 CASE expressionlist2 statementblock-2 CASE ELSE statementblock-n END SELECT Remarks The following list describes the parts of the SELECT CASE statement. ----------------------------------------------------------------------------- ---------------------------------------------------------------------------- testexpression Any numeric or string expression. statementblock-1, statementblock-2, The elements statementblock-1 to statementblock-n statementblock-n consist of any number of statements on one or more lines. expressionlist1, expressionlist2 These elements can have any of the three following forms. expression , expression... expression TO expression IS relational-operator expression The following list describes the parts of expressionlist . ----------------------------------------------------------------------------- ---------------------------------------------------------------------------- expression Any numeric or string expression. The type of the expression must be compatible with the type of testexpression. (The type of the expression will be coerced to the same type as testexpression. For example, if testexpression is an integer, expressionlist can contain a double-precision data type.) relational-operator Any of the following operators. < Less than ---------------------------------------------------------------------------- < Less than <= Less than or equal to > Greater than >= Greater than or equal to < > Not equal = Equal If testexpression matches the expression list associated with a CASE clause, then the statement block following that CASE clause is executed up to the next CASE clause or, for the last one, up to END SELECT. Control then passes to the statement following END SELECT. If you use the TO keyword to indicate a range of values, the smaller value must appear first. For example, the statements associated with the line CASE -1 TO -5 are not executed if testexpression is -4. The line should be written as CASE -5 TO -1. You can use a relational operator only if the IS keyword appears. If CASE ELSE is used, its associated statements are executed only if testexpression does not match any of the other CASE selections. It is a good idea to have a CASE ELSE statement in your SELECT CASE block to handle unforeseen testexpression values. When there is no CASE ELSE statement and no expression listed in the CASE clauses matches testexpression, program execution continues normally. You can use multiple expressions or ranges in each CASE clause. For example, the following line is valid. CASE 1 TO 4, 7 TO 9, 11, 13, IS > MaxNumber% You also can specify ranges and multiple expressions for strings. CASE "everything", "nuts" TO "soup", TestItem$ CASE matches strings that are exactly equal to everything, the current value of TestItem$, or that fall between nuts and soup in alphabetical order. Strings are evaluated according to the ASCII values of their characters. Lowercase letters have larger ASCII values than uppercase letters, so this statement is true. nuts > Nuts > NUTS If an expression appears in more than one CASE clause, only the statements associated with the first appearance of the expression are executed. SELECT CASE statements can be nested. Each SELECT CASE statement must have a matching END SELECT statement. Examples In the first example, SELECT CASE is used to take different actions based on the input value. INPUT "Enter acceptable level of risk (1-10). ", Total SELECT CASE Total CASE IS >= 10 PRINT "Maximum risk and potential return." PRINT "Choose stock investment plan." CASE 6 TO 9 PRINT "High risk and potential return." PRINT "Choose corporate bonds." CASE 2 TO 5 PRINT "Moderate risk and return." PRINT "Choose mutual fund." CASE 1 PRINT "No risk, low return." PRINT "Choose IRA." CASE ELSE PRINT "Response out of range." END SELECT Output Enter acceptable level of risk (1-10). 10 Maximum risk and potential return. Choose stock investment plan. Enter acceptable level of risk (1-10). 0 Response out of range. In the following program, the SELECT CASE statement is used to take different actions based on the ASCII value of a character. ' Function and control key constants. CONST ESC = 27, DOWN = 80, UP = 72, LEFT = 75, RIGHT = 77 CONST HOME = 71, ENDKEY = 79, PGDN = 81, PGUP = 73 CLS PRINT "Press Escape key to end." DO ' Get a function or ASCII key. DO Choice$ = INKEY$ LOOP WHILE Choice$ = "" IF LEN(Choice$) = 1 THEN' Handle ASCII keys. SELECT CASE ASC(Choice$) CASE ESC PRINT "Escape key" END CASE IS < 32, 127 PRINT "Control code" CASE 48 TO 57 PRINT "Digit. "; Choice$ CASE 65 TO 90 PRINT "Uppercase letter. "; Choice$ CASE 97 TO 122 PRINT "Lowercase letter. "; Choice$ CASE ELSE PRINT "Punctuation. "; Choice$ END SELECT ELSE' Convert 2-byte extended code to 1-byte ASCII code. Choice$ = RIGHT$(Choice$, 1) SELECT CASE Choice$ CASE CHR$(DOWN) PRINT "DOWN direction key" CASE CHR$(UP) PRINT "UP direction key" CASE CHR$(PGDN) PRINT "PGDN key" CASE CHR$(PGUP) PRINT "PGUP key" CASE CHR$(HOME) PRINT "HOME key" CASE CHR$(ENDKEY) PRINT "END key" CASE CHR$(RIGHT) PRINT "RIGHT direction key" CASE CHR$(LEFT) PRINT "LEFT direction key" CASE ELSE BEEP END SELECT END IF LOOP